﻿namespace FormCreator.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using Microsoft.Practices.Prism.ViewModel;
    using WPForms.Common.Model;

    /// <summary>
    /// Serves as a view model for form elements in the form structure tree.
    /// </summary>
    public class FormElementViewModel : NotificationObject
    {
        /// <summary>
        /// Backing field for IsExpanded.
        /// </summary>
        private bool isExpanded;

        /// <summary>
        /// Initializes a new instance of the <see cref="FormElementViewModel" /> class.
        /// </summary>
        /// <param name="element">The element.</param>
        public FormElementViewModel(FormElement element)
        {
            this.Element = element;
            if (element is FormSection)
            {
                this.Children = new ObservableCollection<FormElementViewModel>();
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FormElementViewModel" /> class.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="parent">The parent.</param>
        public FormElementViewModel(FormElement element, FormElementViewModel parent)
        {
            this.Parent = parent;
            this.Element = element;
            if (element is FormSection)
            {
                this.Children = new ObservableCollection<FormElementViewModel>();
            }
        }

        /// <summary>
        /// Gets a value indicating whether this instance is a section.
        /// </summary>
        public bool IsSection
        {
            get
            {
                return this.Children != null;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this node is expanded.
        /// </summary>
        public bool IsExpanded
        {
            get
            {
                return this.isExpanded;
            }

            set
            {
                if (value != this.isExpanded)
                {
                    this.isExpanded = value;
                    this.RaisePropertyChanged(() => this.IsExpanded);
                }
            }
        }

        /// <summary>
        /// Gets the element.
        /// </summary>
        public FormElement Element { get; private set; }

        /// <summary>
        /// Gets the name.
        /// </summary>
        public string Name
        {
            get
            {
                return this.Element.GetType().Name;
            }
        }

        /// <summary>
        /// Gets the item's parent
        /// </summary>
        public FormElementViewModel Parent { get; private set; }

        /// <summary>
        /// Gets the children.
        /// </summary>
        public IList<FormElementViewModel> Children { get; private set; }

        /// <summary>
        /// Adds the child after the given item.
        /// </summary>
        /// <param name="child">The child to add.</param>
        /// <param name="after">The after child will be added after this item. If null, it will be added as the firs item.</param>
        public void AddChild(FormElementViewModel child, FormElementViewModel after = null)
        {
            child.Parent = this;
            if (after == null)
            {
                this.Children.Insert(0, child);
            }
            else
            {
                this.Children.Insert(this.Children.IndexOf(after), child);
            }
        }
    }
}
